import { Inject, Injectable } from '@nestjs/common';
import { RedisClientType } from 'redis';

@Injectable()
export class RedisService {
  @Inject('REDIS_CLIENT')
  private redisClient: RedisClientType;

  // List 方法封装
  // 获取列表数据
  async listGet(key: string) {
    return await this.redisClient.lRange(key, 0, -1);
  }

  // 设置列表数据
  async listSet(key: string, value: Array<string>, ttl?: number) {
    // 循环添加列表数据
    for (let i = 0; i < value.length; i++) {
      await this.redisClient.lPush(key, value[i]);
    }

    // 给列表设置过期时间
    if (ttl) {
      await this.redisClient.expire(key, ttl);
    }
  }
}
